import net.spreadshirt.shop.lib.Design;

/**
 * The Designs class stores all design objects
 * and provides some methods to handle the items.
 */
class net.spreadshirt.shop.lib.Designs {

	// A list of all design items.
	private var _designs:Array;

	/**
	 * Designs Constructor
	 */
	public function Designs() {
		clear();
	}

	/**
	 * Clears all values.
	 */
	public function clear():Void {
		_designs = new Array();
	}

	/**
	 * Stores a list of design items.
	 * The old list will be overwritten.
	 * 
	 * @param	items	The list of design items.
	 */
	public function addDesigns(items:Array):Void {
		// Clear design list.
		clear();
		// Adds all items.
		for(var i:Number = 0; i < items.length; i++) {
			addDesign(items[i]);
		}
	}

	/**
	 * Stores a single design item in the articles list.
	 * 
	 * @param	item	A single design item.
	 */
	public function addDesign(item:Object):Void {
		// Only adds the item if there's no other item
		// with the same id already stored.
		if(!isExisting(item)){
			var design:Design = new Design(item);
			_designs.push(design);
		}
	}

	/**
	 * Returns the number of stored design items.
	 * 
	 * @return	The number of items in the design list.
	 */
	public function getCount():Number {
		return _designs.length;
	}

	/**
	 * Returns the complete list of all design items.
	 * 
	 * @return	A list of design items.
	 */
	public function getAllItems():Array {
		return _designs;
	}

	/**
	 * Returns a single design item.
	 * 
	 * @param	id	The design ID.
	 * 
	 * @return	A single design item.
	 */
	public function getItem(id:Number):Design {
		var item:Design;
		for(var i:Number = 0; i < _designs.length; i++) {
			if(id == _designs[i].design_id) {
				item = _designs[i];
			}
		}
		return item;
	}

	/**
	 * Returns a single design item.
	 * 
	 * @param	partner_id	The partner ID.
	 * 
	 * @return	A single design item.
	 */
	public function getDesignItemByPartnerID(partner_id:Number):Design {
		var item:Design;
		for(var i:Number = 0; i < _designs.length; i++) {
			if(partner_id == _designs[i].partner_id) {
				item = _designs[i];
				break;
			}
		}
		return item;
	}

	/**
	 * Checks if the item is already stored.
	 * 
	 * @param	item	The design item.
	 * 
	 * @return	true or false
	 */
	private function isExisting(item:Object):Boolean {
		var found:Boolean = false;
		for(var i:Number = 0; i < _designs.length; i++) {
			if(item.design_id == _designs[i].design_id) {
				found = true;
				break;
			}
		}
		return found;
	}

}
